package com.zut.dao.impl;

import com.zut.dao.ShopDao;
import com.zut.pojo.Shop;
import com.zut.utils.DruidUtil;
import com.zut.vo.ShopVo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author 张晨
 * @date 2024/9/12 13:19
 * @desc
 */
public class ShopDaoImpl implements ShopDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());
    @Override
    public Integer insert(Shop shop) {
        String sql = "insert into shop (shop_name,shop_address,shop_explain,shop_img,shop_category_id,start_price,delivery_price,remarks,start_time,end_time,business_id) values(?,?,?,?,?,?,?,?,?,?,?)";

        return jdbcTemplate.update(sql,shop.getShopName(),shop.getShopAddress(),shop.getShopExplain(),shop.getShopImg(),shop.getShopCategoryId(),shop.getStartPrice(),shop.getDeliveryPrice(),shop.getRemark(),shop.getStartTime(),shop.getEndTime(),shop.getBusinessId());
    }

    @Override
    public List<Shop> selectById(String businessId) {
        String sql = "select * from shop where business_id = ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Shop>(Shop.class), businessId);
    }

    @Override
    public String getCategoryName(Integer shopCategoryId) {
        String sql = "select category_name from shop_category where shop_category_id = ?";
        return jdbcTemplate.queryForObject(sql,String.class,shopCategoryId);
    }
    @Override
    public Integer deleteById(int i) {
        String sql = "delete from shop where shop_id = ?";
        return jdbcTemplate.update(sql,i);
    }

    @Override
    public Integer updateById(Shop shop) {
        String sql = "update shop set shop_name = ?,shop_address = ?,shop_explain = ?,shop_img = ?,shop_category_id = ?,start_price = ?,delivery_price = ?,remarks = ?,start_time = ?,end_time = ?,business_id = ? where shop_id= ? and business_id = ?";
        Integer result = jdbcTemplate.update(sql,shop.getShopName(),shop.getShopAddress(),shop.getShopExplain(),shop.getShopImg(),shop.getShopCategoryId(),shop.getStartPrice(),shop.getDeliveryPrice(),shop.getRemark(),shop.getStartTime(),shop.getEndTime(),shop.getBusinessId(),shop.getShopId(),shop.getBusinessId());
        System.out.println(result);
        return result;


    }

    @Override
    public Shop selectByShopId(int shopId) {
        String sql = "select * from shop where shop_id = ?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Shop>(Shop.class),shopId);
    }
}
